<template>
	<u-popup v-model="show" mode="bottom" border-radius="14" :mask-close-able="false">
		<view class="region-select-bar">
			<view class="cancel-btn" @click="cancel">取消</view>
			<view class="confirm-btn" @click="confirm">确定</view>
		</view>
		<picker-view :indicator-style="indicatorStyle" :value="value" @change="bindChange" class="picker-view">
			<picker-view-column>
				<view class="item" v-for="(item,index) in multiSelector[0]" :key="index">{{item.label}}</view>
			</picker-view-column>
			<picker-view-column>
				<view class="item" v-for="(item,index) in multiSelector[1]" :key="index">{{item.label}}</view>
			</picker-view-column>
			<picker-view-column>
				<view class="item" v-for="(item,index) in multiSelector[2]" :key="index">{{item.label}}</view>
			</picker-view-column>
		</picker-view>
	</u-popup>
</template>

<script>
	import RegionJson from './area.json';
	export default {
		props: {
			value: {
				type: Boolean,
				default: false
			},
			area: {
				type: Object,
				default: () => {
					return {};
				}
			}
		},
		watch: {
			value(val) {
				this.show = val;
			}
		},
		data() {
			return {
				show: false,
				value: [0, 0, 0],
				indicatorStyle: `height: 50px;`,
				multiSelector: [
					[],
					[],
					[]
				],
			};
		},
		created() {
			this.multiSelector[0] = RegionJson
			this.multiSelector[1] = RegionJson[0].children
			this.multiSelector[2] = RegionJson[0].children[0].children
		},
		methods: {
			bindChange: function(e) {
				const val = e.detail.value
				if(this.value[0] !== val[0]){
					this.multiSelector[1] = this.multiSelector[0][val[0]].children
					this.multiSelector[2] = this.multiSelector[0][val[0]].children[0].children
				}else if(this.value[1] !== val[1]){
					this.multiSelector[2] = this.multiSelector[1][val[1]].children
				}
				this.value = val
			},
			cancel() {
				this.$emit('input', false);
			},
			confirm() {
				const province = this.multiSelector[0][this.value[0]]
				const city = this.multiSelector[1][this.value[1]]
				const area = this.multiSelector[2][this.value[2]]
				const params = {
					province: {
						name: province.label,
						code: province.value,
					},
					city: {
						name: city.label,
						code: city.value
					},
					area: {
						name: area.label,
						code: area.value
					}
				}
				this.$emit('confirm', params);
				this.$emit('input', false);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.region-select-bar{
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		.confirm-btn{
			color: $uni-color-primary;
		}
	}
	.picker-view {
		height: 400rpx;
	}
	.item {
		line-height: 100rpx;
		text-align: center;
	}
</style> 